<template>
  <div class="login">
    <el-form ref="form" label-position="top" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type='password' v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="isLoading" @click="onSubmit('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import request from '@/utils/request'
import { login } from '@/services/user'

export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        phone: '18201288771',
        password: '111111'
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入正确手机号', triggrt: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码长度6~18位', trigger: 'blur' }
        ]
      },
      isLoading: false
    };
  },
  methods: {
    async onSubmit (form) {
    //   this.$refs.form.validate(valid => {
    //     console.log(valid)
    //   })
    //   this.$store.commit('add', {
    //     amount: 10
    //   })
    //   this.$store.dispatch('addHandle', {
    //     amount: 12
    //   })
      try {
        // 1 校验成功后的功能
        await this.$refs.form.validate()
        // 2 发送请求
        this.isLoading = true
        // const { data } = await request({
        //   method: 'POST',
        //   url: '/front/user/login',
        //   data: qs.stringify(this.form)
        // })
        const { data } = await login(this.form)
        // 3 相应处理
        if (data.state === 1) {
          this.$message.success('登录成功')
          // 将用户信息存储到vuex中
          this.$store.commit('setUser', data.content)
          // 根据可能存在的redirect数据进行跳转设置
          this.$router.push(this.$route.query.redirect || '/')
        } else {
          this.isLoading = false
          this.$message.error(data.message)
        }
      } catch (err) {
        console.log('没有通过校验')
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.login{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    .el-form{
        background-color: #fff;
        padding: 30px;
        width: 300px;
        border-radius: 4px;
        .el-button{
            width: 100px;
        }
    }
}
</style>
